﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using T.FAS.Runtime.Metadata.Entity.Page;
using T.ServiceComponent.DAC.Service;
using T.ServiceComponent.DataAccessEx.Core;

namespace T.FAS.Runtime.Page.Dao
{
    public class PageTemplateGroupDao : DaoService<PageTemplateGroup>
    {
        public PageTemplateGroupDao() :
           base()
        {
            DBName = "TFAS";
        }

        public override string TableName => "FAS_PageTemplateGroup";
        public override string IDColumnName => "ID";
        public override List<string> ExceptColumnName => new List<string> { "Templates", "Children" };
        public override Dictionary<string, object> GetDataMapping(PageTemplateGroup data)
        {
            var datas = base.GetDataMapping(data);
            datas.Remove("TemplateType");
            datas.Add("TemplateType", (int)(data.TemplateType));
            return datas;
        }
        public override PageTemplateGroup ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            var myCol = new Dictionary<string, PropertyInfo>(base.Columns);
            myCol.Remove("TemplateType");
            var data = base.ConvertToObject(myCol, row);
            if (row["TemplateType"] != DBNull.Value)
                data.TemplateType = (TemplateType)Enum.Parse(typeof(TemplateType),
                                                Convert.ToString(row["TemplateType"]));
            return data;
        }
        /// <summary>
        /// 模板分组
        /// </summary>
        public List<PageTemplateGroup> GetPageTemplateGroup()
        {
            List<PageTemplateGroup> result = new List<PageTemplateGroup>();
            List<object> objects = base.GetObjectsByFilter("pageType not in (8,9)");
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as PageTemplateGroup));
            }
            return result;
        }
        /// <summary>
        /// 模板分组
        /// </summary>
        public List<PageTemplateGroup> GetTemplateGroups(int pageType, int templateType)
        {
            List<PageTemplateGroup> result = new List<PageTemplateGroup>();
            List<IDbDataParameter> paramList = new List<IDbDataParameter>();
            paramList.Add(DB.MakeInParam("@templateType", TDbDataType.Int, templateType));
            paramList.Add(DB.MakeInParam("@pageType", TDbDataType.VarChar, pageType));
            string sql = $"select * from {TableName} where templateType=@templateType and pageType =@pageType and ParentId is not null ";
            var ds = DB.ExecuteDataSet(sql, paramList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    var row = ds.Tables[0].Rows[i];
                    result.Add(ConvertToObject(Columns, row));
                }
            }
            return result;
        }
    }
}
